package com.zbkj.crmeb.merchant.admin.controller;

import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.common.CommonResult;
import com.crmeb.core.exception.CrmebException;
import com.zbkj.crmeb.chant.request.ChannelPartitionCouponRequest;
import com.zbkj.crmeb.chant.response.ChannelPartitionCouponResponse;
import com.zbkj.crmeb.chant.service.ChannelPartitionCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/admin/mer/channel/partition/coupon")
@Api(tags = "瓜分券")
public class MerChannelPartitionCouponController extends AdminBaseController{

    @Autowired
    private ChannelPartitionCouponService channelPartitionCouponService;

    @ApiOperation(value = "保存/修改瓜分券信息")
    @PostMapping("/saveOrUpdate")
    public CommonResult<Boolean> saveOrUpdateInfo(@RequestBody ChannelPartitionCouponRequest request, HttpServletRequest servletRequest) {
        if (request.getPersonNumber() < 2) {
            throw new CrmebException("瓜分好友最少为2人！");
        }
        return channelPartitionCouponService.saveOrUpdateInfo(request, getMerId(servletRequest)) ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation(value = "提前结束")
    @PutMapping("/ahead/{id}")
    public CommonResult<Boolean> earlyTerminationActivity(@PathVariable Integer id) {
        return CommonResult.success(channelPartitionCouponService.earlyTerminationActivity(id));
    }

    @ApiOperation(value = "删除瓜分券")
    @DeleteMapping("/{id}")
    public CommonResult<Boolean> delPartition(@PathVariable Integer id) {
        return CommonResult.success(channelPartitionCouponService.delPartition(id));
    }

    @ApiOperation(value = "瓜分券列表")
    @GetMapping("/list")
    public CommonResult<CommonPage<ChannelPartitionCouponResponse>> getList(ChannelPartitionCouponRequest request, PageParamRequest pageParamRequest, HttpServletRequest servletRequest) {
        return CommonResult.success(channelPartitionCouponService.getList(request, pageParamRequest, getMerId(servletRequest)));
    }

    @ApiOperation(value = "瓜分券详情")
    @GetMapping("/info/{id}")
    public CommonResult<ChannelPartitionCouponResponse> getInfo(@PathVariable Integer id, HttpServletRequest servletRequest) {
        return CommonResult.success(channelPartitionCouponService.getInfo(id, getMerId(servletRequest)));
    }
}
